package com.nccsjz.back.product.web;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import utils.FileUtils;

import  com.nccsjz.back.news.service.NewsService;
import com.nccsjz.pojo.News;
import com.nccsjz.base.BaseAction;
import com.nccsjz.front.service.IndexCardService;
import com.nccsjz.utils.Pager;
@SuppressWarnings("serial")
public class ProductAction extends BaseAction{
    //存放新闻
	private News news;
    //存放id
	private Long newsId;
    //存放url中的参数
	private long id;
	//存放分页信息
    private Pager pager;
    //页码
    private int pageNo=1;
    //每页多少行
    private int pageSize=5;
    //判断标识
    private String flag;
    //存放多选中的id
    private String[] checkbox;
    
    private List propics;//产品展示
    private String filename;//图片名称
    
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public List getPropics() {
		return propics;
	}

	public void setPropics(List propics) {
		this.propics = propics;
	}

	public String[] getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(String[] checkbox) {
		this.checkbox = checkbox;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	private NewsService newsService=null;
	public void setId(long id) {
		this.id = id;
	}

	public Long getNewsId() {
		return newsId;
	}

	public void setNewsId(Long newsId) {
		this.newsId = newsId;
	}
	
	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}
	//获取所有新闻
    public String list_News() throws Exception{
    	newsService=new  NewsService();
    	pager=newsService.getNews(pageSize, pageNo);
    	return SUCCESS;
    }

    //添加新闻 ＊中文乱码问题
    public String add_News() throws Exception{
    	newsService=new NewsService();
    	boolean flag=newsService.addNews(news);
    	if(flag){
    	   return "list";
    	}else{
    		return ERROR;
    	}
    }
    //删除新闻
    public String delete_News() throws Exception{
    	newsService=new NewsService();
    	boolean flag=newsService.deleteNews(id);
    	if(flag){
    	return "list";
    	}else{
    		return ERROR;
    	}
    }
    //查看和修改视图
    public String view_News()throws Exception{
    	if(0!=id&&null!=flag){
        	newsService=new NewsService();
        	news=newsService.getNews(id);
    		return "edit";
    	}else{
        	newsService=new NewsService();
        	news=newsService.getNews(id);
    		return "view";
    	 }
    }
    
    //修改新闻
    public String edit_News() throws Exception{
    	newsService=new NewsService();
    	boolean flag=newsService.updateNews(news);
    	if(flag){
    	 return "list";
    	}else{
    	 return ERROR;
    	}
    }
    //批量删除
    public String deleteBatch_News() throws Exception{
    	newsService=new NewsService();
    	
    	List<Long> list=new ArrayList<Long>();
    	for(String s:checkbox){
    		list.add(Long.parseLong(s));
    	}
    	boolean flag=newsService.deleteBatchNews(list);
    	if(flag){
    	 return "list";
    	}else{
    	 return ERROR;
    	}
    }
    /**
     * 前台，查看新闻具体内容。
     * @return
     * @throws Exception
     */
    public String viewCon_News() throws Exception{
     	newsService=new NewsService();
    	news=newsService.getNews(id);
//    	viewMore_News
		return "viewCon";
    }
    /**
     * 前台，查看"更多"新闻内容。
     * @return
     * @throws Exception
     */
    public String viewMore_News() throws Exception{
    	newsService=new  NewsService();
    	pager=newsService.getNews(pageSize, pageNo);
		return "viewMore";
    }
    
    public String showPicList_Pro(){
    	IndexCardService iservice=new IndexCardService();
    	this.propics=iservice.getProPics(ServletActionContext
				.getServletContext());    	
    	return "showPiclist";
    }
    /**
     * 删除图片文件
     * @return
     */
    public String del_Pro(){
    	FileUtils.deleteFile(ServletActionContext.getServletContext().getRealPath("") + File.separator + "userfiles"
				+ File.separator + "image"+File.separator+filename);
    	IndexCardService iservice=new IndexCardService();
    	this.propics=iservice.getProPics(ServletActionContext
				.getServletContext());
    	return "showPiclist";
    }
}
